package com.pixelengine.Task26Oee;


import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class TheConfig {
    private static String _configFilename="";
    private static TheConfig _s_config = null ;
    public  static TheConfig getInstance() {
        if( _s_config==null ) {
            System.out.println("The config is not inited.");
            System.exit(1);
        }
        return _s_config ;
    }
    public static boolean init(String configJsonfilename) {
        _configFilename = configJsonfilename ;
        Gson gson= new Gson() ;
        try {
            JsonReader reader = new JsonReader(new FileReader(configJsonfilename)) ;
            _s_config = new TheConfig();
            _s_config = gson.fromJson(reader,TheConfig.class) ;

            _s_config.script_wmts_template_content =
                    new String(
                            Files.readAllBytes(Paths.get(_s_config.script_wmts_template)),
                            StandardCharsets.UTF_8);

            //_s_config.testPngData=Files.readAllBytes(Paths.get("placeholder.png"));

            ProductInfos.init( _s_config.product_infos);

            return true ;
        } catch (Exception e) {
            e.printStackTrace();
            return false ;
        }
    }

    public String http_root = "" ;
    public String script_wmts_template = "" ;
    public String script_wmts_template_content = "" ;
    public String script_dir = "" ;
    public byte[] testPngData = null ;
    public String product_infos="";
    public String tile_file_dir="";
    public String roi_dir="";
    public String cog_dir="";
    public String bg_lines="";
    public String style_dir="";
    public int debuginfo = 1;
    //about 50KB each tile.
    public int cache_tile_count=2000;//缓存瓦片数量 2000x50KB=100MB

    //public HashMap<String,String> scriptContentDict = new HashMap<String,String>() ;
    //public HashMap<String,byte[]> roiContentDict = new HashMap<String,byte[]>() ;

    //public synchronized void putRoiData(String roiname, byte[] data) {
    //    roiContentDict.put(roiname,data);
    //}
    Cache<String,byte[]> m_roiCache = Caffeine.newBuilder()
            .maximumSize(100)
            .expireAfterWrite(1,TimeUnit.DAYS)
            .build();
    public byte[] getRoiData(String roiname) throws IOException {
        String fullroiname =roi_dir + roiname ;
        byte[] roicontent = m_roiCache.getIfPresent(fullroiname) ;
        if( roicontent==null ) {
            roicontent = Files.readAllBytes(Paths.get(fullroiname));
            m_roiCache.put(fullroiname,roicontent);
            return roicontent;
        }else{
            return roicontent;
        }
    }


    Cache<String,byte[]> m_styleCache = Caffeine.newBuilder()
            .maximumSize(100)
            .expireAfterWrite(1,TimeUnit.DAYS)
            .build();
    public byte[] getStyleData(String styleName) throws IOException {
        if( styleName.endsWith(".json") == false ) styleName+=".json";
        String fullstylename =style_dir + styleName ;
        byte[] styleContent = m_styleCache.getIfPresent(fullstylename) ;
        if( styleContent==null ) {
            styleContent = Files.readAllBytes(Paths.get(fullstylename));
            m_styleCache.put(fullstylename,styleContent);
            return styleContent;
        }else{
            return styleContent;
        }
    }
}
